<template>
  <a-card style="position: relative">
    <a-row :gutter="12">
      <a-col :span="24">
        <a-steps style="padding: 20px 0" :current="current" @change="onChangeSteps">
          <a-step title="数据源选择" description="" />
          <a-step title="界面配置" description="" />
          <a-step title="查询项配置" description="" />
          <a-step title="完成配置" description="" />
        </a-steps>
      </a-col>
      <a-col :span="6">
        <LeftTree ref="leftTree" @selected="getTreeData"></LeftTree>
      </a-col>
      <a-col :span="18">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col v-for="(item, index) in listData" :key="index" :md="6" :sm="8">
                <a-form-item :label="item.fieldCnName">
                  <div v-if="item.queryType === 'queryLike'">
                    <a-input v-model="item.queryValueStart"></a-input>
                  </div>
                  <div v-else-if="item.queryType === 'queryDate'">
                    <a-range-picker v-model="item.dateArray" style="width: 100%" @change="
                      (value, dateString) => {
                        handlePanelChange(value, dateString, index)
                      }
                    " />
                  </div>
                  <div v-else-if="item.queryType === 'queryCompare'">
                    <a-select style="width: 45%" placeholder="请选择" v-model="item.queryValueStart">
                      <a-select-option value=">">大于</a-select-option>
                      <a-select-option value="<">小于</a-select-option>
                      <a-select-option value="=">等于</a-select-option>
                      <a-select-option value=">=">大于等于</a-select-option>
                      <a-select-option value="<=">小于等于</a-select-option>
                    </a-select>
                    <a-input-number style="margin-left: 5%; width: 50%" v-model="item.queryValueEnd"></a-input-number>
                  </div>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
          :pagination="ipagination" :loading="loading" @change="handleTableChange">
        </a-table>
      </a-col>
    </a-row>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import LeftTree from './LeftTree'
import { httpAction, getAction, postAction } from '@/api/manage'
import moment from 'moment'
import 'moment/locale/zh-cn'
import _ from 'lodash'

export default {
  props: ['id', 'type'],
  name: 'mxsjzs',
  components: {
    LeftTree
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      queryParam: {
        listData: ''
      },
      superParam: {
        modelId: ''
      },
      listData: [],
      selectedData: '',
      radioValue: '',
      columns: columns(this),
      url: {
        list: '/dataModel/findResultList',
        getSearchAndTitle: '/dataModel/queryTitleAndSearch'
      },
      current: 0,
      dateArray: [],
      getListData: [],
      disableMixinCreated: true
    }
  },
  created() { },
  computed: {},
  methods: {
    searchReset() {
      this.dataSource = []
      this.dateArray = []
      this.listData = _.cloneDeep(this.getListData)
      this.queryParam = this.$options.data().queryParam
      this.ipagination = this.$options.data().ipagination
    },
    onChangeSteps(current) {
      console.log('onChangeSteps:', current)
      // this.current = current;
    },
    getTreeData(data) {
      this.superParam.modelId = data
      this.getSearchAndTitleList(data)
    },
    getSearchAndTitleList(id) {
      this.listData = []
      this.dataSource = []
      this.columns = columns(this)
      getAction(this.url.getSearchAndTitle, { id: id }).then(res => {
        this.loading = false
        if (res.success) {
          res.result.searchList.map((item, index) => {
            this.listData.push({
              queryType: item.searchType,
              queryKey: item.fieldEnName,
              fieldCnName: item.fieldCnName,
              dateArray: [],
              queryValueStart: '',
              queryValueEnd: ''
            })

            this.getListData = _.cloneDeep(this.listData)
          })
          res.result.titleList.map((item, index) => {
            this.columns.push({
              title: item.fieldCnName,
              align: 'center',
              dataIndex: item.fieldEnName
            })
          })
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    loadData(arg) {
      debugger
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      console.log('columns', this.columns)
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.queryParam.listData = JSON.stringify(_.cloneDeep(this.listData))
      var params = this.getQueryParams() //查询条件
      this.loading = true
      postAction(this.url.list, params).then(res => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    moment,
    // 得到年份选择器的值
    handlePanelChange(value, dateString, index) {
      if (value.length) {
        this.listData[index].queryValueStart = moment(value[0]).format('YYYY-MM-DD')
        this.listData[index].queryValueEnd = moment(value[1]).format('YYYY-MM-DD')
      } else {
        this.listData[index].queryValueStart = ""
        this.listData[index].queryValueEnd = ""
      }

    }
  }
}
</script>

<style scoped>

</style>